﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using System.Data.SqlClient;
using CMD_WEB.DAL;
using Newtonsoft.Json;

namespace CMD.BAL.SOL
{
    [DataContractAttribute]
    public class InsertSap
    {

        public static string insertSapSol(string DatosSap)
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            JavaScriptSerializer ms2 = new JavaScriptSerializer();
            int j = 0;
            try
            {
                Dictionary<string, Dictionary<string, string>> values = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(DatosSap);
                //Dictionary<string, string> SAP = JsonConvert.DeserializeObject<Dictionary<string, string>>(values["SAP"].ToString());
                //Dictionary<string, string> mensajeSAP = JsonConvert.DeserializeObject<Dictionary<string, string>>(values["mensajeSAP"].ToString());
                DataSet result = new DataSet();

                SqlCommand sp = new SqlCommand("SP_CMD_INSERT_SAP");
                sp.CommandType = CommandType.StoredProcedure;
                string solic = "", secu="", unid="";

                foreach (KeyValuePair<string, string> d in (Dictionary<string, string>)values["SAP"])
                {
                    sp.Parameters.AddWithValue(d.Key, d.Value);
                    if (d.Key == "SOLIC") solic = d.Value;
                    if (d.Key == "SECU") solic = d.Value;
                    if (d.Key == "UNID_SOLICITUD") unid = d.Value;
                }
                
                result = SrvDB.ExecuteCommand(sp);
                result = new DataSet();
                sp = new SqlCommand("SP_CMD_INSERT_SAP_MESSAGE");
                sp.CommandType = CommandType.StoredProcedure;

                if (values.ContainsKey("mensajeSAP"))
                {
                    foreach (KeyValuePair<string, string> d in (Dictionary<string, string>)values["mensajeSAP"])
                    {
                        sp.Parameters.AddWithValue(d.Key, d.Value);
                    }
                }
                else
                {
                    sp.Parameters.AddWithValue("UNID_SOLICITUD", unid);
                    sp.Parameters.AddWithValue("SOLIC", solic);
                    sp.Parameters.AddWithValue("SECU", secu);
                    sp.Parameters.AddWithValue("STAT", "");
                    sp.Parameters.AddWithValue("KUNNR", "");
                    sp.Parameters.AddWithValue("MSGNO", "");
                    sp.Parameters.AddWithValue("MSGID", "");
                    sp.Parameters.AddWithValue("MSGTY", "");
                    sp.Parameters.AddWithValue("MSGV1", "");
                    sp.Parameters.AddWithValue("MSGV2", "");
                    sp.Parameters.AddWithValue("MSGV3", "");
                    sp.Parameters.AddWithValue("MSGV4", "NO SE DEVOLVIERON REGISTROS DESDE SAP");
                }
                result = SrvDB.ExecuteCommand(sp);
                return result.Tables.Count.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
